{# Hidden meta for JS to read the total count #}
<div id="assetLogsMeta" data-count="{{ logs_count|default:0 }}" class="visually-hidden"></div>

{# Optional: set this True when reusing on non-asset-detail pages #}
{# Example from another page: {% include 'assets/partials/asset_logs_table.html' with show_asset_info=True %} #}
{% if show_asset_info is not defined %}
  {% with False as show_asset_info %}{% endwith %}
{% endif %}

<div class="table-responsive">
  <table class="table table-hover table-sm align-middle mb-0">
    <thead class="table-light">
      <tr>
        <th style="width: 160px;">时间</th>
        {% if show_asset_info %}
          <th style="min-width: 220px;">资产</th>
        {% endif %}
        <th style="width: 110px;">操作</th>
        <th style="width: 160px;">操作人</th>
        <th>描述</th>
        <th style="width: 130px;">IP</th>
        <th style="width: 160px;">操作</th>
      </tr>
    </thead>
    <tbody>
      {% for log in logs_page %}
      <tr>
        <td><small>{{ log.created_at|date:"Y-m-d H:i:s" }}</small></td>

        {% if show_asset_info %}
          <td>
            {% if log.asset %}
              <a href="{% url 'assets:asset_detail' log.asset_id %}">
                {{ log.asset.name }}
                {% if log.asset.asset_number %}
                  <small class="text-muted ms-1">#{{ log.asset.asset_number }}</small>
                {% endif %}
              </a>
            {% else %}
              <span class="text-muted">-</span>
            {% endif %}
          </td>
        {% endif %}

        <td>
          {# Action badge with visual differentiation #}
          {% with a=log.action %}
            {% if a == 'create' %}
              <span class="badge bg-success">创建</span>
            {% elif a == 'update' %}
              <span class="badge bg-primary">更新</span>
            {% elif a == 'allocate' %}
              <span class="badge bg-info">分配</span>
            {% elif a == 'transfer' %}
              <span class="badge bg-warning text-dark">转移</span>
            {% elif a == 'scrap' or a == 'delete' %}
              <span class="badge bg-danger">报废</span>
            {% elif a == 'maintenance' %}
              <span class="badge bg-warning text-dark">维护</span>
            {% elif a == 'inventory' %}
              <span class="badge bg-secondary">盘点</span>
            {% elif a == 'borrow' %}
              <span class="badge bg-teal" style="background-color:#20c997;">借用</span>
            {% elif a == 'return' %}
              <span class="badge bg-dark">归还</span>
            {% elif a == 'qr_generate' or a == 'label_print' %}
              <span class="badge bg-secondary">标签/二维码</span>
            {% elif a == 'status_change' or a == 'location_change' or a == 'user_change' or a == 'department_change' %}
              <span class="badge bg-secondary">变更</span>
            {% elif a == 'image_upload' %}
              <span class="badge bg-success">图片上传</span>
            {% elif a == 'image_delete' %}
              <span class="badge bg-danger">图片删除</span>
            {% else %}
              <span class="badge bg-secondary">{{ log.get_action_display }}</span>
            {% endif %}
          {% endwith %}
        </td>

        <td>
          {% if log.operator %}
            <i class="fas fa-user text-muted me-1"></i>
            {{ log.operator.get_full_name|default:log.operator.username }}
          {% else %}
            <span class="text-muted">系统</span>
          {% endif %}
        </td>

        <td>
          {% if log.description %}
            <div class="text-truncate" style="max-width: 520px;" title="{{ log.description }}">{{ log.description }}</div>
          {% else %}
            <span class="text-muted">-</span>
          {% endif %}
        </td>

        <td>
          <code class="text-muted">{{ log.ip_address|default:"" }}</code>
        </td>
        <td>
          <div class="d-flex gap-2">
            <a class="btn btn-sm btn-outline-primary" href="{% url 'assets:asset_log_detail' log.id %}"><i class="fas fa-eye"></i> 详情</a>
            {% if perms.assets.delete_assetlog %}
            <form method="post" action="{% url 'assets:asset_log_delete' log.id %}" onsubmit="return confirm('确定删除该日志记录吗？此操作不可恢复。');">
              {% csrf_token %}
              <button type="submit" class="btn btn-sm btn-outline-danger"><i class="fas fa-trash"></i> 删除</button>
            </form>
            {% endif %}
          </div>
        </td>
      </tr>
      {% empty %}
      <tr>
        <td colspan="{% if show_asset_info %}7{% else %}6{% endif %}" class="text-center text-muted py-4">
          <i class="fas fa-inbox fa-2x d-block mb-2"></i>
          暂无日志记录
        </td>
      </tr>
      {% endfor %}
    </tbody>
  </table>
</div>

{% if logs_page.has_other_pages %}
<div class="d-flex justify-content-between align-items-center p-2 border-top">
  <div class="text-muted small">
    显示第 {{ logs_page.start_index }} - {{ logs_page.end_index }} 条，共 {{ logs_page.paginator.count }} 条
  </div>
  <nav aria-label="Asset logs pagination">
    <ul class="pagination pagination-sm mb-0">
      {% if logs_page.has_previous %}
        <li class="page-item">
          <a class="page-link" href="?{{ qs_base }}{% if qs_base %}&{% endif %}page={{ logs_page.previous_page_number }}">上一页</a>
        </li>
      {% endif %}
      {% for num in logs_page.paginator.page_range %}
        {% if logs_page.number == num %}
          <li class="page-item active"><span class="page-link">{{ num }}</span></li>
        {% elif num > logs_page.number|add:'-3' and num < logs_page.number|add:'3' %}
          <li class="page-item">
            <a class="page-link" href="?{{ qs_base }}{% if qs_base %}&{% endif %}page={{ num }}">{{ num }}</a>
          </li>
        {% endif %}
      {% endfor %}
      {% if logs_page.has_next %}
        <li class="page-item">
          <a class="page-link" href="?{{ qs_base }}{% if qs_base %}&{% endif %}page={{ logs_page.next_page_number }}">下一页</a>
        </li>
      {% endif %}
    </ul>
  </nav>
</div>
{% endif %}